/*******************************************************************************
 * Copyright (c) 2004, 2005 Elias Volanakis and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Elias Volanakis - initial API and implementation
 *******************************************************************************/
package edu.ccut.saturn.studio.logic.parts;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.ConnectionLocator;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editparts.AbstractConnectionEditPart;
import org.eclipse.gef.editpolicies.ConnectionEditPolicy;
import org.eclipse.gef.editpolicies.ConnectionEndpointEditPolicy;
import org.eclipse.gef.requests.GroupRequest;

import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.logic.model.Connection;
import edu.ccut.saturn.studio.logic.model.BasicShape;
import edu.ccut.saturn.studio.logic.model.commands.ConnectionDeleteCommand;
import edu.ccut.saturn.studio.logic.model.commands.OpenConnectionCommand;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnDecisionNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnSwichNode;

class ConnectionEditPart extends AbstractConnectionEditPart implements
		PropertyChangeListener {

	public void activate() {
		if (!isActive()) {
			super.activate();
			((BasicShape) getModel()).addPropertyChangeListener(this);
		}
	}
	
	public void performRequest(Request req) {
		if (req.getType() == RequestConstants.REQ_OPEN) {
			getViewer().getEditDomain().getCommandStack().execute(
					new OpenConnectionCommand(((Connection) getModel())
							.getDialog(), (Connection) getModel()));
		}
	}

	protected void createEditPolicies() {
		// Selection handle edit policy.
		// Makes the connection show a feedback, when selected by the user.
		installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE,
				new ConnectionEndpointEditPolicy());
		// Allows the removal of the connection model element
		installEditPolicy(EditPolicy.CONNECTION_ROLE,
				new ConnectionEditPolicy() {
					protected Command getDeleteCommand(GroupRequest request) {
						return new ConnectionDeleteCommand(getCastedModel());
					}
				});
	}

	protected IFigure createFigure() {
		PolylineConnection connection = (PolylineConnection) super.createFigure();
		connection.setTargetDecoration(new PolygonDecoration()); 
		connection.setLineStyle(getCastedModel().getLineStyle()); 
		connection.setForegroundColor(getCastedModel().getColor());
		if (getCastedModel().getLineName() != null&&getCastedModel().getSource() instanceof SaturnSwichNode||getCastedModel().getLineName() != null&&getCastedModel().getSource() instanceof SaturnDecisionNode) {
			if("".equals(getCastedModel().getLineName())){
				getCastedModel().setLineName(Messages.getString("ConnectionEditPart.failedname")); //$NON-NLS-1$
			}
			Label connLabel = new Label(getCastedModel().getLineName());
			connLabel.setBorder(new LineBorder());
			connLabel.setOpaque(true);
			connLabel.setBackgroundColor(ColorConstants.tooltipBackground);
			connection.add(connLabel,
					new ConnectionLocator(connection,
							ConnectionLocator.MIDDLE));
		}
		return connection;
	}

	public void deactivate() {
		if (isActive()) {
			super.deactivate();
			((BasicShape) getModel()).removePropertyChangeListener(this);
		}
	}

	private Connection getCastedModel() {
		return (Connection) getModel();
	}

	public void propertyChange(PropertyChangeEvent event) {
		String property = event.getPropertyName();
		if (Connection.LINESTYLE_PROP.equals(property)) {
			((PolylineConnection) getFigure()).setLineStyle(getCastedModel()
					.getLineStyle());
		}else if (Connection.LINE_NAME_PROPERTY.equals(property)) {
			refreshConnectionFigure();
		}
	}
	
	private void refreshConnectionFigure() {

		Label connLabel = new Label(getCastedModel().getLineName());
		connLabel.setBorder(new LineBorder());
		connLabel.setOpaque(true);
		connLabel.setBackgroundColor(ColorConstants.tooltipBackground);
		List<?> d = ((PolylineConnection) getFigure()).getChildren();
		for (int i = 0; i < d.size(); i++) {
			if (d.get(i) instanceof Label) {
				((PolylineConnection) getFigure()).remove((IFigure) d.get(i));
			}
		}
		((PolylineConnection) getFigure()).add(connLabel,
				new ConnectionLocator(((PolylineConnection) getFigure()),
						ConnectionLocator.MIDDLE));

	}

}